/*
 * main-view.c
 *
 *  Created on: 10-dic-2008
 *      Author: igor
 */

#include "view.h"

/* TODO - next two functions will be used in a second time to
 * associate some actions to anchorblock.
 */
static void
my_anchorblock_bt(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *av = data;
   elm_anchorblock_hover_end(av);
}

static void
my_anchorblock_anchor(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *av = data;
   Elm_Entry_Anchorblock_Info *ei = event_info;
   Evas_Object *bt, *bx;

   bt = elm_button_add(obj);
   elm_button_label_set(bt, ei->name);
   elm_hover_content_set(ei->hover, "middle", bt);
   evas_object_show(bt);

   // hints as to where we probably should put hover contents (buttons etc.).
   if (ei->hover_top)
     {
	bx = elm_box_add(obj);
	bt = elm_button_add(obj);
	elm_button_label_set(bt, "Top 1");
	elm_box_pack_end(bx, bt);
	evas_object_smart_callback_add(bt, "clicked", my_anchorblock_bt, av);
	evas_object_show(bt);
	bt = elm_button_add(obj);
	elm_button_label_set(bt, "Top 2");
	elm_box_pack_end(bx, bt);
	evas_object_smart_callback_add(bt, "clicked", my_anchorblock_bt, av);
	evas_object_show(bt);
	bt = elm_button_add(obj);
	elm_button_label_set(bt, "Top 3");
	elm_box_pack_end(bx, bt);
	evas_object_smart_callback_add(bt, "clicked", my_anchorblock_bt, av);
	evas_object_show(bt);
	elm_hover_content_set(ei->hover, "top", bx);
	evas_object_show(bx);
     }
   if (ei->hover_bottom)
     {
	bt = elm_button_add(obj);
	elm_button_label_set(bt, "Bot");
	elm_hover_content_set(ei->hover, "bottom", bt);
	evas_object_smart_callback_add(bt, "clicked", my_anchorblock_bt, av);
	evas_object_show(bt);
     }
   if (ei->hover_left)
     {
	bt = elm_button_add(obj);
	elm_button_label_set(bt, "Left");
	elm_hover_content_set(ei->hover, "left", bt);
	evas_object_smart_callback_add(bt, "clicked", my_anchorblock_bt, av);
	evas_object_show(bt);
     }
   if (ei->hover_right)
     {
	bt = elm_button_add(obj);
	elm_button_label_set(bt, "Right");
	elm_hover_content_set(ei->hover, "right", bt);
	evas_object_smart_callback_add(bt, "clicked", my_anchorblock_bt, av);
	evas_object_show(bt);
     }
}
/* END - anchorblock management */

/*
 * show over buttons of bt option in main win
 */
static void
my_hover_bt_1(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *hv = data;
   evas_object_show(hv);
}


/* finestra principale
 *
 * main win
 */
void
my_win_main(void)
{

   printf("DEBUG: <my_win_main>\n");
   Evas_Object *win, *bg, *av, *sc, *bx, *bb, *ic, *bx2, *bt, *en, *ly;
   Evas_Object *hv, *bx3;
   char buf[PATH_MAX];

   win = elm_win_add(NULL, PACKAGE, ELM_WIN_BASIC);
   elm_win_title_set(win, PACKAGE " " VERSION);
   evas_object_smart_callback_add(win, "delete-request", my_win_del, NULL);
   elm_win_autodel_set(win, 1);

   bg = elm_bg_add(win);
   elm_win_resize_object_add(win, bg);
   evas_object_size_hint_weight_set(bg, 1.0, 1.0);
   evas_object_show(bg);

   ly = elm_layout_add(win);
   snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
   printf ("DEBUG layout: %s;\n", buf);
   elm_layout_file_set(ly, buf, "main");
//   evas_object_size_hint_weight_set(ly, 1.0, 1.0);
   elm_win_resize_object_add(win, ly);
   evas_object_show(ly);

   // titolo: nome della categoria
   // title: cat name
   edje_object_part_text_set(elm_layout_edje_get(ly), "cat_name", catname);

   // FIXME - se non inseriti in un box questi pulsanti non appaiono
   // senza il tema edj
   Evas_Object *bt_add, *bt_del, *bt_opt;

   bt_add = elm_button_add(win);
   elm_button_label_set(bt_add, "New");
   evas_object_smart_callback_add(bt_add, "clicked", my_entry_bt_new_item, win);
   evas_object_size_hint_align_set(bt_add, -1.0, -1.0);
   evas_object_size_hint_weight_set(bt_add, 1.0, 0.0);
   window_swallow(ly, "button_add", bt_add);
   evas_object_show(bt_add);

   bt_del = elm_button_add(win);
   elm_button_label_set(bt_del, "Delete");
   evas_object_smart_callback_add(bt_del, "clicked", my_entry_bt_delete, win);
   window_swallow(ly, "button_del", bt_del);
   evas_object_size_hint_align_set(bt_del, -1.0, -1.0);
   evas_object_size_hint_weight_set(bt_del, 1.0, 0.0);
   evas_object_show(bt_del);

   /* hover button */
   hv = elm_hover_add(win);
//   elm_hover_style_set(hv, "popout");

   bt_opt = elm_button_add(win);
   elm_button_label_set(bt_opt, "Options");
   evas_object_smart_callback_add(bt_opt, "clicked", my_hover_bt_1, hv);
   window_swallow(ly, "button_opt", bt_opt);
   evas_object_size_hint_align_set(bt_opt, -1.0, -1.0);
   evas_object_size_hint_weight_set(bt_opt, 1.0, 0.0);
   evas_object_show(bt_opt);


   sc = elm_scroller_add(win);
//   evas_object_size_hint_weight_set(sc, 1.0, 0.0);
   elm_win_resize_object_add(win, sc);

   bx = elm_box_add(win);
   evas_object_size_hint_weight_set(bx, 1.0, 1.0);		// espande orizzontalmente
//   evas_object_size_hint_align_set(bx, -1.0, -1.0);
   elm_scroller_content_set(sc, bx);
   evas_object_show(bx);
   evas_object_resize(bx, 320, 300);

   int i = 0;
   struct todoitem *todolist = NULL;
   todolist = (struct todoitem *) leggi_items_da_db(todolist);

   if (todolist == NULL)
   {
      printf("DEBUG: lista vuota;\n");
   } else {

      while (todolist != NULL)
      {
//       printf("%s \t%s\n%s\n", todolist->inf.title, todolist->inf.infos, todolist->inf.text);

	     ic = elm_icon_add(win);
//	     snprintf(buf, sizeof(buf), "%s/icons/%s.png", PIXMAPS_DIR, todolist->inf.icon);
	     snprintf(buf, sizeof(buf), "%s/%s.png", PIXMAPS_DIR, todolist->inf.icon);
	     printf("DEBUG icon: %s ;\n", buf);

	     elm_icon_file_set(ic, buf, NULL);
	     evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
	     //	     elm_icon_scale_set(ic, 1.0, 1.0);

	     bb = elm_bubble_add(win);
	     elm_bubble_label_set(bb, todolist->inf.title);
	     elm_bubble_info_set(bb, todolist->inf.infos);
	     elm_bubble_icon_set(bb, ic);

	     // assegna un'azione all'icona: modifica la nota
	     evas_object_smart_callback_add(ic, "clicked", my_win_modify_item, todolist);
	     evas_object_show(ic);

	     evas_object_size_hint_weight_set(bb, 1.0, 1.0);
	     evas_object_size_hint_align_set(bb, -1.0,-1.0);	// espande orizzontalmente

	     av = elm_anchorblock_add(win);
	     elm_anchorblock_hover_style_set(av, "popout");
	     elm_anchorblock_hover_parent_set(av, win);
	     elm_anchorblock_text_set(av, todolist->inf.text);
	     evas_object_smart_callback_add(av, "anchor,clicked", my_anchorblock_anchor, av);
	     elm_bubble_content_set(bb, av);
	     evas_object_show(av);

	     elm_box_pack_end(bx, bb);
	     elm_layout_content_set(ly, "item", bb);
	     evas_object_show(bb);

	     // elemento successivo
	     todolist = todolist->pun;

	 }
   }

   window_swallow(ly, "contenitore_bx", bx);
   evas_object_show(bx);
   window_swallow(ly, "contenitore_sc", sc);
   evas_object_show(sc);
   evas_object_resize(win, 320, 300);

   /* cut */
   elm_hover_parent_set(hv, win);
   elm_hover_target_set(hv, bt_opt);

   bx3 = elm_box_add(win);

   bt = elm_button_add(win);
   elm_button_label_set(bt, "Refresh");
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_refresh, win);
   evas_object_size_hint_align_set(bt, -1.0, -1.0);
   evas_object_size_hint_weight_set(bt, 1.0, 0.0);
   elm_box_pack_end(bx3, bt);
   evas_object_show(bt);


   bt = elm_button_add(win);
   elm_button_label_set(bt, "Modify");
   evas_object_smart_callback_add(bt, "clicked", my_entry_win_modify, win);
   evas_object_size_hint_align_set(bt, -1.0, -1.0);
   evas_object_size_hint_weight_set(bt, 1.0, 0.0);
   elm_box_pack_end(bx3, bt);
   evas_object_show(bt);

   bt = elm_button_add(win);
   elm_button_label_set(bt, "Change Cat");
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_changecat, win);
   evas_object_size_hint_align_set(bt, -1.0, -1.0);
   evas_object_size_hint_weight_set(bt, 1.0, 0.0);
   elm_box_pack_end(bx3, bt);
   evas_object_show(bt);

   bt = elm_button_add(win);
   elm_button_label_set(bt, "Delete Cat");
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_deletecat, win);
   evas_object_size_hint_align_set(bt, -1.0, -1.0);
   evas_object_size_hint_weight_set(bt, 1.0, 0.0);
   elm_box_pack_end(bx3, bt);
   evas_object_show(bt);

   evas_object_show(bx3);
   elm_hover_content_set(hv, "top", bx3);

   evas_object_show(bx3);

   // FIXME: not exported
   elm_widget_focus_set(win, 1);
   evas_object_show(win);
}
